package com.uchengwang.ui.cm_ui.login.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.SchoolConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.adapter.SwitchSchoolAdapter;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolListBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/5/31.
 * 选择学校
 */

public class ChooseSchoolActivity extends BaseWithTitleActivity{
    private RecyclerView rv;
    private PtrClassicFrameLayout refresh;
    private SwitchSchoolAdapter adapter;
    private RecyclerAdapterWithHF mAdapter;
    private int page, totlaPagers;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_school);
        setTitle("选择院校");
        initView();
    }

    private void initView() {
        refresh = (PtrClassicFrameLayout) findViewById(R.id.switch_refresh);

        rv = (RecyclerView) findViewById(R.id.switch_lv);

        initData();

    }

    private void initData(){
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SwitchSchoolAdapter(this, true);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        rv.setAdapter(mAdapter);

        refresh.setAutoLoadMoreEnable(true);
        refresh.setLoadMoreEnable(true);

        refresh.postDelayed(new Runnable() {

            @Override
            public void run() {
                refresh.autoRefresh(true);
            }
        }, 150);

        refresh.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                refresh.loadMoreComplete(true);
                getSchools(true);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getSchools(false);
                } else {
                    refresh.loadMoreComplete(false);
                }
            }
        });

    }

    private void getSchools(final boolean isRefresh){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 20);
        HttpManager.newInstance(this).query(SchoolConfig.GetSchools.method_get, SchoolConfig.GetSchools.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (isRefresh){
                    refresh.refreshComplete();
                }else {
                    refresh.loadMoreComplete(true);
                }
                UCommonUtil.getUtil().showToast(ChooseSchoolActivity.this, "获取失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                if (isRefresh){
                    refresh.refreshComplete();
                }else {
                    refresh.loadMoreComplete(true);
                }
                if (status != 0){
                    UCommonUtil.getUtil().showToast(ChooseSchoolActivity.this, msg);
                    return;
                }

                if (!TextUtils.isEmpty(data)){
                    SchoolListBean bean = JsonUtils.getManager().stringToBean(data, SchoolListBean.class);
                    if (bean != null){
                        totlaPagers = bean.getTotalPage();
                        List<SchoolBean> list = bean.getData();
                        if (list != null && list.size() >0){

                            if (isRefresh){
                                adapter.setData(list);
                            }else {
                                adapter.getData().addAll(list);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                }
            }
        });


    }
}
